package cn.iocoder.yudao.module.myerp.controller.admin.SKUproduct;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.myerp.controller.admin.SKUproduct.vo.*;
import cn.iocoder.yudao.module.myerp.dal.dataobject.SKUproduct.ErpProductSkuDO;
import cn.iocoder.yudao.module.myerp.service.SKUproduct.ErpProductSkuService;

@Tag(name = "管理后台 - ERP SKU级产品")
@RestController
@RequestMapping("/myerp/erp-product-sku")
@Validated
public class ErpProductSkuController {

    @Resource
    private ErpProductSkuService erpProductSkuService;

    @PostMapping("/create")
    @Operation(summary = "创建ERP SKU级产品")
    @PreAuthorize("@ss.hasPermission('myerp:erp-product-sku:create')")
    public CommonResult<Long> createErpProductSku(@Valid @RequestBody ErpProductSkuSaveReqVO createReqVO) {
        return success(erpProductSkuService.createErpProductSku(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新ERP SKU级产品")
    @PreAuthorize("@ss.hasPermission('myerp:erp-product-sku:update')")
    public CommonResult<Boolean> updateErpProductSku(@Valid @RequestBody ErpProductSkuSaveReqVO updateReqVO) {
        erpProductSkuService.updateErpProductSku(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除ERP SKU级产品")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('myerp:erp-product-sku:delete')")
    public CommonResult<Boolean> deleteErpProductSku(@RequestParam("id") Long id) {
        erpProductSkuService.deleteErpProductSku(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得ERP SKU级产品")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('myerp:erp-product-sku:query')")
    public CommonResult<ErpProductSkuRespVO> getErpProductSku(@RequestParam("id") Long id) {
        ErpProductSkuDO erpProductSku = erpProductSkuService.getErpProductSku(id);
        return success(BeanUtils.toBean(erpProductSku, ErpProductSkuRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得ERP SKU级产品分页")
    @PreAuthorize("@ss.hasPermission('myerp:erp-product-sku:query')")
    public CommonResult<PageResult<ErpProductSkuRespVO>> getErpProductSkuPage(@Valid ErpProductSkuPageReqVO pageReqVO) {
        PageResult<ErpProductSkuDO> pageResult = erpProductSkuService.getErpProductSkuPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ErpProductSkuRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出ERP SKU级产品 Excel")
    @PreAuthorize("@ss.hasPermission('myerp:erp-product-sku:export')")
    @OperateLog(type = EXPORT)
    public void exportErpProductSkuExcel(@Valid ErpProductSkuPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ErpProductSkuDO> list = erpProductSkuService.getErpProductSkuPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "ERP SKU级产品.xls", "数据", ErpProductSkuRespVO.class,
                        BeanUtils.toBean(list, ErpProductSkuRespVO.class));
    }

}